package com.jn.okhttp.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.jn.ok_http_demo.R;
import com.jn.okhttp.adapter.HotSearchAdapter;
import com.jn.okhttp.base.BaseActivity;
import com.jn.okhttp.contract.HotsearchContact;
import com.jn.okhttp.entity.HotSearchEntity;
import com.jn.okhttp.presenter.HotSearchPresenter;

import java.util.ArrayList;
import java.util.List;

public class HotSearchActivity extends BaseActivity implements HotsearchContact.IHotView {

    private HotsearchContact.IHotSearchPresenter mPresenter;

    private Spinner mSpinnerPlatform;
    private RecyclerView mRecyclerView;
    private HotSearchAdapter mAdapter;
    private List<HotSearchEntity.HotItem> mlist;
    private FloatingActionButton HotSearch,Translate,Zidian;

    @Override
    public int getLayoutId() {
        return R.layout.hotsearch;
    }


    @Override
    public void init() {
        mPresenter = new HotSearchPresenter(this, this);
        mSpinnerPlatform = findViewById(R.id.spinner_location);
        mRecyclerView = findViewById(R.id.recyclerView1);

        // 设置Spinner的监听器
        mSpinnerPlatform.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String[] platforms = getResources().getStringArray(R.array.spinner);
                String selectedPlatform = platforms[position];
                fetchData(selectedPlatform);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Do nothing
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mlist = new ArrayList<>();
        mAdapter = new HotSearchAdapter(mlist);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void fetchData(String platform) {
        // 将中文平台名称转换为对应的英文标识
        String type = getPlatformType(platform);
        if (type != null) {
            mPresenter.getData(type);
        } else {
            Toast.makeText(this, "未知平台类型", Toast.LENGTH_SHORT).show();
        }
    }

    private String getPlatformType(String platform) {
        switch (platform) {
            case "百度":
                return "baidu";
            case "知乎":
                return "zhihu";
            case "微信":
                return "weixin";
            case "哔哩哔哩":
                return "bilibili";
            case "抖音":
                return "douyin";
            case "腾讯新闻":
                return "news";
            case "历史上的今天":
                return "lishi";
            case "今日头条":
                return "toutiao";
            case "微博":
                return "weibo";
            default:
                return null;
        }
    }

    @Override
    public void showData(HotSearchEntity result) {

        mlist = result.getResult();
        mAdapter.updateData(mlist);
    }

    @Override
    public void showError(String result) {

    }

    @Override
    public void onHttpLoading(boolean loading) {
        // 可以在这里显示或隐藏加载指示器
    }

    @Override
    public void onHttpFail(String... msg) {
        if (msg != null && msg.length > 0) {
            Toast.makeText(this, "请求失败: " + msg[0], Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onHttpTimeout() {
        Toast.makeText(this, "请求超时", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onHttpTokenExpiration() {
        Toast.makeText(this, "Token过期", Toast.LENGTH_SHORT).show();
    }



}